package com.achilles.learnmessenger;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private LinearLayout mLinearLayout ;
    private Button mButton;

    private Messenger mService;
    private boolean isConn;
    private int numberA = 0;
    private int numberB ;

    private Messenger mMessenger = new Messenger(new Handler(){
        @Override
        public void handleMessage(Message msgFromServer) {
            super.handleMessage(msgFromServer);
            switch (msgFromServer.what) {
                case MessengerService.MSG_SUM:{
                    TextView tv = (TextView) findViewById(msgFromServer.arg1);
                    tv.setText(tv.getText() + "=" + msgFromServer.arg2);
                    break;
                }
            }
        }
    });

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = new Messenger(service);
            isConn = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
            isConn = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bindServiceInvoked();
        mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout);
        mButton = (Button) findViewById(R.id.btn);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int a = numberA++;
                int b = (int) (Math.random() * 100);

                TextView tv = new TextView(MainActivity.this);
                tv.setText(a + "+" + b + " = caculating ...");
                tv.setId(a);
                mLinearLayout.addView(tv);

                Message msgFromClient = Message.obtain(null,MessengerService.MSG_SUM,a,b);
                msgFromClient.replyTo = mMessenger;

                if (isConn){
                    try {
                        mService.send(msgFromClient);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    private void bindServiceInvoked(){
        Intent intent = new Intent(this,MessengerService.class);
        bindService(intent,mConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mConnection);
    }
}
